Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var e = require("../../@babel/runtime/helpers/classCallCheck"), t = require("../../@babel/runtime/helpers/createClass"), n = require("../../@babel/runtime/helpers/typeof");

require("../../@babel/runtime/helpers/Arrayincludes");

var i = [ "miniprogram", "h5" ];

function s(e) {
    return i.includes(e) ? e : "object" !== ("undefined" == typeof window ? "undefined" : n(window)) && "function" != typeof XMLHttpRequest ? "miniprogram" : "h5";
}

var r = function() {
    function n(t) {
        var i = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : null;
        e(this, n), this.env = s(t), this.cookie = i, this.baseUrl = "https://kapi.10010.com/";
    }
    return t(n, [ {
        key: "fetch",
        value: function(e) {
            var t = this;
            if ("h5" === this.env) {
                var n = new XMLHttpRequest(), i = e.method, s = void 0 === i ? "GET" : i, r = e.url, o = e.data, a = void 0 === o ? null : o;
                return new Promise(function(e, i) {
                    n.onreadystatechange = function() {
                        4 === n.readyState && (200 === n.status ? e(n.responseText) : i(n.status));
                    };
                    var o = t.baseUrl + r;
                    n.open(s, o), n.withCredentials = !0, n.setRequestHeader("Content-Type", "application/json"), 
                    "GET" === s ? a = null : a && (a = JSON.stringify(a)), n.send(a);
                });
            }
            var u = e.method, c = void 0 === u ? "GET" : u, l = e.url, d = e.data, h = void 0 === d ? null : d;
            return new Promise(function(e, n) {
                var i = t.baseUrl + l;
                if (!t.cookie) {
                    var s = getApp();
                    t.cookie = s.globalData.token;
                }
                wx.request({
                    url: i,
                    method: c,
                    header: {
                        "content-type": "application/json",
                        Cookie: "mini_program_cookie=".concat(t.cookie)
                    },
                    data: h,
                    success: function(t) {
                        e(t.data);
                    },
                    error: function(e) {
                        n(e);
                    }
                });
            });
        }
    }, {
        key: "get",
        value: function(e) {
            return this.fetch({
                url: e
            }).catch(function(e) {
                return console.error(e);
            });
        }
    }, {
        key: "post",
        value: function(e, t) {
            return this.fetch({
                method: "POST",
                url: e,
                data: t
            }).catch(function(e) {
                return console.error(e);
            });
        }
    } ]), n;
}(), o = function() {
    function n(t) {
        var i, o;
        if (e(this, n), this.defaultOptions = {
            env: null,
            seq: "" + Date.now() + (i = 1, o = 1e8, Math.floor(Math.random() * (o - i + 1)) + i),
            action_id: "default",
            scene: "",
            extra_data: "",
            cookie: null,
            toast: !1,
            success: function() {},
            error: function() {}
        }, this.options = Object.assign({}, this.defaultOptions, t), this.env = s(this.options.env), 
        this.Service = new r(this.env, this.options.cookie), "h5" === this.env) {
            if (this.useDefaultBtn = !1, !t.el) throw new Error("容器ID为必传参数");
            var a = document.getElementById(t.el);
            if (!a) throw new Error("未找到指定ID的容器");
            this.options.text && this.options.style || (this.useDefaultBtn = !0), a.innerHTML = this.generateH5Code(), 
            this.bindH5Event();
        } else this.bindMiniEvent();
    }
    return t(n, [ {
        key: "generateH5Code",
        value: function() {
            var e = this.useDefaultBtn ? "" : '<script type="text/wxtag-template">\n        <style>\n          .wx-subscribe-btn{'.concat(this.options.style, '}\n        </style>\n        <div class="wx-subscribe-btn">').concat(this.options.text, "</div><\/script>");
            return '<wx-open-subscribe id="wx-subscribe-btn" template="'.concat(this.options.templateId, '">').concat(e, "</wx-open-subscribe>");
        }
    }, {
        key: "request",
        value: function(e) {
            var t = this.options, n = t.seq, i = t.action_id, s = t.scene, r = t.extra_data;
            return this.Service.post("wx-subscribe-notify/outer/user-subscribe-result", {
                env: this.env,
                event_detail: e,
                seq: n,
                action_id: i,
                scene: s,
                extra_data: r
            });
        }
    }, {
        key: "bindH5Event",
        value: function() {
            var e = this, t = this.options, n = t.success, i = t.error, s = document.getElementById("wx-subscribe-btn");
            s.addEventListener("success", function(t) {
                var i = t.detail;
                e.request({
                    errMsg: i.errMsg,
                    subscribeDetails: i.subscribeDetails
                }).then(function(e) {
                    n(t.detail, e);
                }).catch(function(e) {
                    n(e.detail, e);
                });
            }), s.addEventListener("error", function(e) {
                i(e.detail);
            });
        }
    }, {
        key: "bindMiniEvent",
        value: function() {
            var e = this, t = this.options, n = t.templateId, i = t.success, s = t.error, r = t.toast, o = n.split(",");
            wx.requestSubscribeMessage({
                tmplIds: o,
                success: function(t) {
                    var n = !o.every(function(e) {
                        return "reject" === t[e];
                    }), s = JSON.stringify(o.reduce(function(e, n) {
                        var i = t[n];
                        return i && (e[n] = {
                            status: i
                        }), e;
                    }, {}));
                    e.request({
                        errMsg: t.errMsg,
                        subscribeDetails: s
                    }).then(function(e) {
                        "0000" === e.code ? (r && n && wx.showToast({
                            title: "订阅成功",
                            icon: "none",
                            duration: 2e3
                        }), i(t, e, n)) : (wx.showToast({
                            title: e.msg || e.message || "操作失败，请稍后再试!",
                            icon: "none",
                            duration: 2e3
                        }), i(t, e));
                    }).catch(function(e) {
                        r && wx.showToast({
                            title: e.msg || e.message || "操作失败，请稍后再试!",
                            icon: "none",
                            duration: 2e3
                        }), i(t, e);
                    });
                },
                fail: function(e) {
                    s(e);
                }
            });
        }
    } ]), n;
}();

exports.default = o;